package com.bst.common.base.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.bst.common.base.service.IBaseDbPubService;
import com.bst.common.exception.base.BaseException;
import com.bst.common.vo.BaseDbPubVO;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.Date;

@Service
public class BaseDbPubServiceImpl implements IBaseDbPubService {

    @Override
    public List<Map<String,Object>> queryDataBySql(DruidDataSource datasource, String sql) throws BaseException {
        Connection connection=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            connection = datasource.getConnection();
            st=(Statement) connection.createStatement();
            rs = st.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();// 获取列的数量
            List<Map<String,Object>> dataList = new ArrayList<>();
            Map<String,Object> rowData;
            while (rs.next()) {
                rowData = new HashMap<>();
                for (int i = 1; i <= count; i++) {
                    rowData.put(rsmd.getColumnLabel(i),rs.getString(i));
                }
                dataList.add(rowData);
            }

            return dataList;
        }catch (Exception e) {
            throw new BaseException("查询错误"+e.getMessage());
        } finally {
            try {
                if(rs!=null && !rs.isClosed()) {
                    rs.close();
                }
                if(st!=null && !st.isClosed()) {
                    st.close();
                }
                if(connection!=null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public BaseDbPubVO querySingleDataBySql(DruidDataSource datasource, String sql) throws BaseException {
        Connection connection=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            connection = datasource.getConnection();
            st=(Statement) connection.createStatement();
            Long beginTimes = new Date().getTime();
            rs = st.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();// 获取列的数量
            List<Map<String,Object>> dataList = new ArrayList<>();
            Map<String,Object> rowData;
            rs.next();
            rowData = new HashMap<>();
            for (int i = 1; i <= count; i++) {
                rowData.put(rsmd.getColumnLabel(i),rs.getString(i));
            }
            return new BaseDbPubVO(rowData, new Date().getTime()-beginTimes);
        }catch (Exception e) {
            throw new BaseException("查询错误"+e.getMessage());
        } finally {
            try {
                if(rs!=null && !rs.isClosed()) {
                    rs.close();
                }
                if(st!=null && !st.isClosed()) {
                    st.close();
                }
                if(connection!=null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public Map<String, BaseDbPubVO> querySingleDataByIdDb(DruidDataSource datasource, Map<String,String> sqls, String valFld) throws BaseException {
        Connection connection=null;
        Statement st=null;
        ResultSet rs=null;
        Map<String,BaseDbPubVO> returnData = new LinkedHashMap<>();
        String sql=null;
        Long beginTiems,endTimes;
        try {
            connection = datasource.getConnection();
            st=(Statement) connection.createStatement();
            List<Map<String,Object>> dataList;
            for(Map.Entry<String,String> entry:sqls.entrySet()) {
                beginTiems = new Date().getTime();
                sql = entry.getValue();
                rs = st.executeQuery(sql);
                dataList = resultSetToMap(rs);
                if(dataList!=null && dataList.size()>0) {
                    for (Map<String, Object> map : dataList) {
                        returnData.put(entry.getKey(),new BaseDbPubVO(map.get(valFld),new Date().getTime()-beginTiems));
                    }
                }
            }
            return returnData;
        }catch (Exception e) {
            throw new BaseException("查询错误"+e.getMessage()+"。sql："+sql);
        } finally {
            try {
                if(rs!=null && !rs.isClosed()) {
                    rs.close();
                }
                if(st!=null && !st.isClosed()) {
                    st.close();
                }
                if(connection!=null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    private List<Map<String,Object>> resultSetToMap(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();// 获取列的数量
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String,Object> rowData;
        while (rs.next()) {
            rowData = new HashMap<>();
            for (int i = 1; i <= count; i++) {
                rowData.put(rsmd.getColumnLabel(i),rs.getString(i));
            }
            dataList.add(rowData);
        }
        return dataList;
    }
}
